////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Julien Etienne.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
package org.siam.impl.data.formatting {
	
	import mx.formatters.CurrencyFormatter;
	import mx.formatters.DateFormatter;
	import mx.formatters.Formatter;
	import mx.formatters.NumberFormatter;
	import mx.formatters.PhoneFormatter;
	import mx.formatters.ZipCodeFormatter;
	
	import org.siam.core.data.formatting.IFormatter;
	import org.siam.core.data.formatting.IFormatterRegistry;
	import org.siam.core.data.formatting.IFormattingManager;
	import org.siam.impl.data.formatting.formatters.NumberFormatter;
	import org.siam.utils.ClassUtils;
	
	/**
	* 
	*
	**/
	public class FormattingManager implements IFormattingManager {

		private static const flexFormatters:Array = [
			mx.formatters.NumberFormatter, 
			mx.formatters.CurrencyFormatter, 
			mx.formatters.DateFormatter, 
			mx.formatters.PhoneFormatter, 
			mx.formatters.ZipCodeFormatter
		];
		private static const siamFormatters:Array = [
			org.siam.impl.data.formatting.formatters.NumberFormatter
		];

		private var _registry:IFormatterRegistry;

		public function FormattingManager() {
			_registry = new FormatterRegistry();
		}
		
		/**
		* 
		*
		**/
		public function format(value:Object, format:String):String {
			return _registry.find(format).format(value);
		}
		
		public static function newInstance(configuration:XML):IFormattingManager {
			var instance:FormattingManager = new FormattingManager();
			for each (var formatter:XML in configuration["formatter"]) {
				var formatterType:String = formatter["@type"];
				var formatterClass:Class = ClassUtils.resolveName(formatterType);
				for each (var format:XML in formatter["format"]) {
					var formatName:String = format["@name"];
					var formatString:String = format["@formatString"];
					instance.registerFormatter(formatName, formatterClass, formatString); 
				}
			}
			return instance;			
		}
		
		private function registerFormatter(name:String, type:Class, formatString:String=null):void {
			var formatter:IFormatter = null;
			
			var newInstance:* = new type();
			if (newInstance is IFormatter) {
				formatter = IFormatter(newInstance);
			} else if (newInstance is mx.formatters.Formatter) {
				formatter = new FormatterWrapper(Formatter(newInstance));
			}
			if (formatter) {
				formatter.formatString = formatString;
				_registry.register(name, formatter);
			}
		}
	}
}